package com.guodu.service.impl;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.guodu.dao.MaterialJoinStoreDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.MaterialJoinStore;
import com.guodu.entity.MaterialjoinstoreStatus;
import com.guodu.entity.Store;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.MaterialJoinStoreService;
import com.guodu.vo.MaterialJoinStoreQueryVo;
import com.guodu.vo.MaterialJoinStoreVo;

@Service("materialJoinStoreService")
public class MaterialJoinstoreServiceImpl implements MaterialJoinStoreService {

	@Resource
	private MaterialJoinStoreDao materialJoinStoreDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Override
	public List<MaterialJoinStoreVo> findByVo(MaterialJoinStoreQueryVo materialJoinStoreQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<MaterialJoinStore> list = materialJoinStoreDao.findByVo(materialJoinStoreQueryVo,units, curPage, pageSize);
		List<MaterialJoinStoreVo> materialJoinStoreVos = new ArrayList<MaterialJoinStoreVo>();
		for(MaterialJoinStore m :list){
			MaterialJoinStoreVo materialJoinStoreVo = new MaterialJoinStoreVo(m);
			UserInfo userInfo  = userInfoDao.findById(m.getCreatePeople());
			materialJoinStoreVo.setUserName(userInfo.getUserName());
			if(userInfo.getUnitsId()!=null){
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			if(unitsInfo!=null){
				materialJoinStoreVo.setUnitsLevel(unitsInfo.getUnitsLevel());
			}
			}
			materialJoinStoreVos.add(materialJoinStoreVo);
		}
		return materialJoinStoreVos;
	}

	@Override
	public int findSizeByVo(MaterialJoinStoreQueryVo materialJoinStoreQueryVo,Units units) {
		// TODO Auto-generated method stub
		return materialJoinStoreDao.findSizeByVo(materialJoinStoreQueryVo,units);
	}

	@Override
	public MaterialJoinStore findById(String id) {
		// TODO Auto-generated method stub
		return materialJoinStoreDao.findById(id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		materialJoinStoreDao.del(id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		materialJoinStoreDao.delByIds(ids);
	}

	@Override
	public void save(MaterialJoinStore entity) {
		// TODO Auto-generated method stub
		materialJoinStoreDao.save(entity);
	}

	@Override
	public void examine(String ids,String reason,String beforeStatus,Long afterStatus,UserInfo userInfo) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<MaterialjoinstoreStatus> entities = new ArrayList<MaterialjoinstoreStatus>();
		for(int i=0;i<idses.length;i++){
			MaterialjoinstoreStatus entity = new MaterialjoinstoreStatus();
			entity.setAfterStatus(afterStatus);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(userInfo.getUserId());
			entity.setMaterialId(idses[i]);
			entity.setReason(reason);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		materialJoinStoreDao.changeStatus(entities);
		materialJoinStoreDao.examine(ids,afterStatus);
	}

	@Override
	public void update(MaterialJoinStore entity) {
		// TODO Auto-generated method stub
		materialJoinStoreDao.update(entity);
	}

	@Override
	public List<MaterialjoinstoreStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return materialJoinStoreDao.findStatus(id);
	}

	@Override
	public void delByStoreId(String storeId) {
		// TODO Auto-generated method stub
		materialJoinStoreDao.delByStoreId(storeId);
	}

	@Override
	public Store findStoreById(String id) {
		// TODO Auto-generated method stub
		return materialJoinStoreDao.findStoreById(id);
	}



}
